import {
	knowledgeSeekList,
	knowledDelete,
	knowledcombine,
	updateQaStatus,
	knowledgeClassification,
	updateQACategory,
	exportQA,
	knowledgeImport,
	getQaTemplate,
} from "@/services/knowledgemanagement";

export default {
	namespace: "qaList1",
	state: {
		total: null,
		items: [],
		loading: false,
	},

	effects: {
		*fetchListBySearch({ payload, callback }, { call, put }) {
			const response = yield call(knowledgeSeekList, payload);

			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*delListByQaId({ payload, callback }, { call, put }) {
			const response = yield call(knowledDelete, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*combine({ payload, callback }, { call, put }) {
			const response = yield call(knowledcombine, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*stopQaByQaId({ payload, callback }, { call, put }) {
			const response = yield call(updateQaStatus, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*categoryListJson({ payload, callback }, { call, put }) {
			const response = yield call(knowledgeClassification, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*moveQaCategory({ payload, callback }, { call, put }) {
			const response = yield call(updateQACategory, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*exportQAById({ payload, callback }, { call, put }) {
			const response = yield call(exportQA, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*importQaByUpFile({ payload, callback }, { call, put }) {
			const response = yield call(knowledgeImport, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
		*getImportQaTemp({ payload, callback }, { call, put }) {
			const response = yield call(getQaTemplate, payload);
			if (callback && typeof callback === "function") {
				callback(response);
			}
			yield put({
				type: "save",
				payload: response,
			});
		},
	},

	reducers: {
		save(state, { payload }) {
			return {
				...state,
				...payload,
			};
		},
	},
};
